package com.mys.maggie.nacos.plugin.pause;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.registry.NacosRegistration;
import com.alibaba.nacos.api.exception.NacosException;
import com.mys.maggie.nacos.plugin.AbstractEndPoint;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
@Endpoint(id = "graceful-pause")
public class GracefulPause extends AbstractEndPoint {
    
    protected final transient Logger log = LoggerFactory.getLogger(getClass());
    
    @Autowired
    NacosDiscoveryProperties nacosDiscoveryProperties;
    
    @Autowired
    NacosRegistration nacosRegistration;
    
    @Autowired
    AbstractEnvironment environment;
    
    @Autowired
    HttpServletRequest request;
    
    @WriteOperation
    public String down() {
        if(request != null){
            String remoteAddr = null;
            // 如果是本机下负载,获取不到request会报错,不影响
            try {
                remoteAddr = request.getRemoteAddr();
                String requestURL = request.getRequestURL().toString();
                if(!(requestURL.startsWith("http://localhost") || requestURL.startsWith("https://localhost")
                        || requestURL.startsWith("localhost"))){
                    log.info("[下负载路径异常]==>requestURL:{}",requestURL);
                    return "error";
                }
            } catch (Exception e) {
            }
            log.info("[下负载request ip]==>remoteAdd:{}",remoteAddr);
            String ip = nacosDiscoveryProperties.getIp();
            String serviceName = nacosDiscoveryProperties.getService();
            String groupName = nacosDiscoveryProperties.getGroup();
            String clusterName = nacosDiscoveryProperties.getClusterName();
            String portStr = StringUtils.isBlank(environment.getProperty("server.port")) ? "8080" : environment.getProperty("server.port");
            int port = (nacosDiscoveryProperties.getPort() == -1) ? Integer.parseInt(portStr) : nacosDiscoveryProperties.getPort();
            String namespace = nacosDiscoveryProperties.getNamespace();
            
            log.info("[下负载]自主==> Namespace:{}, ServiceName:{}, Address:{}:{}, Group:{}, ClusterName:{}", namespace, serviceName, ip, port, groupName, clusterName);
            try {
                nacosRegistration.getNacosNamingService().deregisterInstance(serviceName, groupName, ip, port, clusterName);
            } catch (NacosException e) {
                log.error("[下负载]自主==> " + e.getMessage(), e);
                return "error";
            }
        }
        return "success";
    }
    
}